
#define LOG_TAG TAG_MMGR
#include "include/mmgr_log.h"
#include "include/mmgr_wifi.h"




struct wifi_scan_t wifi_cps = {0};                        /* wifi信息 */
uint8_t            wifi_count = 0;                        /* wifi有效热点个数 */
uint8_t            wifi_scan_start = 0;

/**
 * @brief wifi 扫描启动
 * @return int32_t 
 */
int32_t mmgr_wifi_scan_start(void)
{
    int32_t res = 0;
    
    if(wifi_scan_start)
        return 0;

    wifi_cps.rssi_thr = -90;
    wifi_cps.mac_thr = 5;
    wifi_cps.wifi_scan_inr = 10;
    wifi_cps.wifi_scan_en = true;
    if (NULL == wifi_cps.info.rssi) {
        wifi_cps.info.rssi = qlmalloc(sizeof(int) * 21);
        memset(wifi_cps.info.rssi, 0, sizeof(int) * 21);
        wifi_cps.info.mac = qlmalloc(sizeof(char *) * 20);
        memset(wifi_cps.info.mac, 0, sizeof(char *) * 20);
        for (int i = 0; i < 20; i++) {
            wifi_cps.info.mac[i] = qlmalloc(sizeof(char) * 18);
            // log_v("wifi_cps.info.mac[%d] = %p\r\n", i, wifi_cps.info.mac[i]);
            memset(wifi_cps.info.mac[i], 0, sizeof(char) * 18);
        }
    }
    
    if (0 == (res = qltask_modem_ioctl(MDM_CFG_WIFI_SCAN, &wifi_cps))) {
        wifi_scan_start = 1;
    }

    return res;
}


/**
 * @brief wifi 扫描停止
 * @return int32_t 
 */
int32_t mmgr_wifi_scan_stop(void)
{
    if(!wifi_scan_start)
        return 0;

    wifi_cps.wifi_scan_en = false;
    wifi_scan_start = 0;
    return qltask_modem_ioctl(MDM_CFG_WIFI_SCAN, &wifi_cps);
}



/**
 * @brief 获取WiFi信息
 * @return struct wifi_info* 
 */
struct wifi_info *mmgr_wifi_info_get(void)
{
    return &wifi_cps.info;
}

/**
 * @brief 读取WiFi热点个数
 * @return uint8_t 
 */
uint8_t mmgr_wifi_count_get(void)
{
    return wifi_count;
}

/**
 * @brief 设置WiFi热点个数
 * @return uint8_t 
 */
void mmgr_wifi_count_set(uint8_t count)
{
    wifi_count = count;
    mmgr_wifi_scan_stop(); // 单次WIFI扫描
}